package api;

import model.Article;
import model.ArticleDao;
import model.User;
import model.UserDao;
import view.HtmlGenerator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ArticleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        resp.setContentType("text/html; charset=utf-8");
        if(session == null){// 说明没 登录 (那就不能到这个页面);
            String html = HtmlGenerator.getMessagePage("请先登录","login.html");
            resp.getWriter().write(html);
            return ;
        }
        User user = (User) session.getAttribute("user");
        // 通过get 亲求
        // 判断请求中是否存在articleId 参数
        String articleIdStr = req.getParameter("articleId");

        if(articleIdStr == null){
            // a) 没有这个字段就去执行获取文章列表操作
            getAllArticle(user,resp);
        }else {
            // b) 有这个字段就去执行获取文章详情操作
            getOneArticle(Integer.parseInt(articleIdStr),user,resp);
        }
    }

    // 实现新增文章
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // 1.判定登录状态
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession(false);
        resp.setContentType("text/html; charset=utf-8");
        if(session == null){// 说明没 登录 (那就不能到这个页面);
            String html = HtmlGenerator.getMessagePage("请先登录","login.html");
            resp.getWriter().write(html);
            return ;
        }
        // 从请求中读取
        User user = (User) session.getAttribute("user");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if(title == null || "".equals(title) || content == null || "".equals(content)){
            String html = HtmlGenerator.getMessagePage("提交的标题或内容为空!","article");
            resp.getWriter().write(html);
            return ;
        }
        // 把数据写到数据库中
        ArticleDao articleDao = new ArticleDao();
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setUserId(user.getUserId());
        articleDao.add(article);
        // 写一个 发布成功
        String html = HtmlGenerator.getMessagePage("发布成功!","article");
        resp.getWriter().write(html);
        return;
    }

    private void getOneArticle(int articleId, User user, HttpServletResponse resp) throws IOException {
        // 查找数据库
        ArticleDao articleDao = new ArticleDao();
        Article article = articleDao.selectById(articleId);
        if(article == null) {
            String html = HtmlGenerator.getMessagePage("该文章不存在或者已被删除!","article");
            resp.getWriter().write(html);
            return ;
        }
        // 找的就要显示一波
        UserDao userDao = new UserDao();
        User author = userDao.selectById(article.getUserId());
        if(author == null){ // 查不到这个人
            String html = HtmlGenerator.getMessagePage("当前文章已被作者删除","article");
            resp.getWriter().write(html);
            return ;
        }
        String html = HtmlGenerator.getArticleDetailPage(article,user,author);
        resp.getWriter().write(html);
    }

    private void getAllArticle(User user, HttpServletResponse resp) throws IOException {
        // 1. 查找数据库
        ArticleDao articleDao = new ArticleDao();
        List<Article> articles = articleDao.selectAll();
        // 2.构造页面
        String html = HtmlGenerator.getArticleListPage(articles,user);
        resp.getWriter().write(html);
    }
}
